package com.wyl.mine.wallet

import android.content.Intent
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.onFailed
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.ACacheHelper
import com.wyl.base.BeeCloudHelper
import com.wyl.base.MineTiantong
import com.wyl.base.userData
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.toast
import com.wyl.libbase.utils.KLog
import com.wyl.mine.R
import com.wyl.mine.databinding.WalletActivityBinding
import com.wyl.mine.unique.credit.CreditListActivity

class WalletActivity : BindingActivity<WalletActivityBinding>() {
    val gold = ObservableField("0")
    val price = ObservableField("0")
    val index = ObservableInt(0)
    val type = ObservableInt(1) // 1支付宝 2微信

    override fun getLayoutId(): Int = R.layout.wallet_activity

    override fun initView() {
        binding.vm = this

        gold.set(userData?.gold)

        binding.titleBar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                CommonTitleBar.ACTION_RIGHT_TEXT ->
                    startActivity(Intent(this, CreditListActivity::class.java))
                else -> {
                }
            }
        }
    }

    fun checkPrice(i: Int) {
        price.set(list[i]["money"])
        index.set(i)
    }

    fun checkType(i: Int) {
        type.set(i)
    }

    val list = ObservableArrayList<Map<String, String>>()

    override fun loadData() {
        OkGo.post<BaseResponse<List<Map<String, String>>>>(MineTiantong)
            .converter(object : JsonConvert<BaseResponse<List<Map<String, String>>>>() {})
            .adapt(ObservableBody<BaseResponse<List<Map<String, String>>>>())
            .doOnSubscribe { showLoadingDialog() }
            .doFinally { dismissLoadingDialog() }
            .map { it.data }
            .subscribe(
                {
                    list.addAll(it)
                    price.set(it[0]["money"])
                },
                { onFailed(it) }
            ).toDisposables(disposables)

    }

    fun commitString(i: String) = "立即支付${i}元"

    fun commit() {
        if (list.isEmpty()) return

        val number = list[index.get()]["number"]
        val money = list[index.get()]["money"]
        when (type.get()) {
            1 -> BeeCloudHelper.getInstance().payZhiFuBao(
                this, number, "0.1", ACacheHelper.id, result
            )
            2 -> BeeCloudHelper.getInstance().payWeiXin(
                this, number, "0.1", ACacheHelper.id, result
            )
        }
    }

    private val result = object : BeeCloudHelper.Result {
        override fun onSuccess() {
            runOnUiThread {
                toast("支付成功")
                setResult(RESULT_OK)
                onBackPressed()
            }
        }

        override fun onCancel() {

        }

        override fun onFailed(msg: String) {
            runOnUiThread {
                KLog.d(msg)
                toast("支付失败")
            }
        }

    }


}
